package com.mdoya.controller;

import com.alibaba.fastjson.JSON;
import com.mdoya.dto.DishDto;
import com.mdoya.exception.BusinessExceptiion;
import com.mdoya.query.BaseQuery;
import com.mdoya.response.ResultResponse;
import com.mdoya.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author mdoya
 * @version 1.0
 * @description: 菜品表controller控制层
 */
@Slf4j
@RequestMapping("/dish")
@RestController
public class DishController {
    @Autowired
    private DishService dishService;

    /**
     *  添加菜品
     * @param dishDto 临时接受前端参数  JSON
     * @return
     */
    @PostMapping
    public ResultResponse add(@RequestBody DishDto dishDto){
        log.info("新增菜品: {}",dishDto);
        Integer rows = dishService.add(dishDto);
        return rows > 0 ? ResultResponse.success("添加菜品成功") : ResultResponse.error("添加菜品失败");
    }
    /**
     *  更新菜品
     * @param dishDto 临时接受前端参数  JSON
     * @return
     */
    @PutMapping
    public ResultResponse updateById(@RequestBody DishDto dishDto){
        log.info("更新菜品: {}",dishDto);
        System.err.println("更新菜品");
        Integer rows = dishService.updateById(dishDto);
        return rows > 0 ? ResultResponse.success("更新菜品成功") : ResultResponse.error("更新菜品失败");
    }

    /**
     *  查询分页
     * @param baseQuery 分页参数 条件查询
     * @return
     */
    @GetMapping("/page")
    public ResultResponse selectPage(BaseQuery baseQuery){
        log.info("查询分页", baseQuery);
        return ResultResponse.success(dishService.selectPage(baseQuery));
    }

    /**
     *  菜品批量删除 标识状态
     * @param ids
     * @return
     */
    @PostMapping("/batchdelete")
    public ResultResponse batchDeleteIds(@RequestBody List<Long> ids){
        log.info("批量删除菜品: {}",ids);
        if (ids.size() < 0){
            throw new BusinessExceptiion(-1,"请选择要删除菜品");
        }
        Integer rows = dishService.batchDeleteIds(ids);
        return rows > 0 ? ResultResponse.success("批量删除菜品成功") : ResultResponse.error("批量删除菜品失败");
    }

    /**
     *  批量上线和批量停售
     * @param map
     * @return
     */
    @PostMapping("/batchSaleIds")
    public ResultResponse batchSaleIds(@RequestBody Map<String,Object> map){
        log.info("批量销售情况: {}",map);

       /* System.err.println(map);
        String  id = (String) map.get("id");
        System.err.println(id);
        String status = (String) map.get("status");
        System.err.println(status);*/

        Integer rows = dishService.batchSaleIds(map);
        return rows > 0 ? ResultResponse.success("批量处理成功") : ResultResponse.error("批量处理失败");
    }

   /* @GetMapping("/list")
    public ResultResponse selectTypeCategoryListTaocan(Long categoryId){
        log.info("categoryId分类查询数据: {}",categoryId);
        return ResultResponse.success(dishService.selectTypeCategoryList(categoryId));
    }*/

    /**
     *  前端菜品展示
     * @param categoryId
     * @return
     */
    @GetMapping("/list")
    public ResultResponse selectTypeCategoryListTaocan(Long categoryId){
        log.info("categoryId分类查询数据: {}",categoryId);
        return ResultResponse.success(dishService.selectTypeCategoryList(categoryId));
    }
}
